package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Reference
    private FoodService foodService;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Food> data = new ArrayList<>();
            for (String[] strings : list) {
                //膳食名称
                String name = strings[0];
                //膳食说明
                String remark = strings[1];
                //食用尺寸
                String size = strings[2];
                //适用年龄
                String age = strings[3];
                Food food = new Food(name, remark, size, age);
                data.add(food);
            }
            foodService.addList(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }


    //新增膳食库
    @RequestMapping("/add")
    public Result add(@RequestBody Food food) {
        try {
            foodService.add(food);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.ADD_FOOD_SUCCESS);
    }

    //查询膳食库
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return foodService.pageQuery(queryPageBean);
    }

    //删除膳食库
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            foodService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_FOOD_SUCCESS);
    }

    //编辑膳食库
    @RequestMapping("/edit")
    public Result edit(@RequestBody Food food) {
        try {
            foodService.edit(food);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_FOOD_SUCCESS);
    }

    //查询一个膳食库
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Food food = foodService.findById(id);
            return new Result(true, MessageConstant.QUERY_FOOD_SUCCESS,food);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_FOOD_FAIL);
        }
    }

    //查所有
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Food> list = foodService.findAll();
            return new Result(true, MessageConstant.QUERY_FOOD_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_FOOD_FAIL);
        }
    }
}
